

define("charoite/jsonrest",
    ["dojo/_base/xhr", "dojo/json", "dojo/_base/declare"],
    function(xhr, JSON, declare) {
        return declare("charoite.jsonrest", null, {

            //accepts: String
            //Defines the Accept header to use on HTTP requests
            accepts: "application/javascript, application/json",

            make_headers : function(options)
            {
                var headers = {
                    "Content-Type": "application/json",
                    Accept: this.accepts
                };

                if(typeof options != 'undefined' && typeof options.headers != 'undefined')
                {
                    dojo.mixin(headers, options.headers);
                }

                return headers;
            },

            make_xhr: function(method, url, object, options)
            {
                var xhr_options = {
                    url: url,
                    handleAs: "json",
                    headers: this.make_headers(options)
                };

                if(object != null)
                {
                    xhr_options.postData = JSON.stringify(object);
                }

                return xhr(method, xhr_options);
            },

            constructor: function(options)
            {
                declare.safeMixin(this, options);
            },

            get: function(url, options)
            {
                return this.make_xhr("GET", url, null, options);
            },

            put: function(url, object, options)
            {
                return this.make_xhr("PUT", url, object, options);
            },

            post: function(url, object, options)
            {
                return this.make_xhr("POST", url, object, options);
            },

            remove: function(url, object, options)
            {
                return this.make_xhr("DELETE", url, null, options);
            }

        });
    });